﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Genesis
{
    class iTunes
    {
        public string[,] Song = new string[1,7] {{"","","","","","0","0"}};

        public string tmp = "";

        public void LoadLibrary(string libPath, string tmpLib)
        {
            int count = 0;
            XmlDocument iLibrary = new XmlDocument();

            File.Copy(libPath, tmpLib, true);

            string[] oldContents = File.ReadAllLines(tmpLib);
            string[] newContents = new string[oldContents.Length - 2];
            for (int i = 2; i < oldContents.Length; i++)
                newContents[i - 2] = oldContents[i];

            File.WriteAllLines(tmpLib, newContents);

            iLibrary.Load(tmpLib);

            XmlNodeList Key = iLibrary.GetElementsByTagName("key");

            // 0 - Name
            // 1 - Artist
            // 2 - Album
            // 3 - Genre
            // 4 - Location
            // 5 - Time
            // 6 - Count
            foreach (XmlNode node in Key)
            {
                XmlElement x = (XmlElement)node;

                if (x.InnerText == "Playlists")
                    break;
                else
                {
                    if (x.InnerText == "Kind")
                    {
                        if (x.NextSibling.InnerText == "MPEG-4 video file")
                        {
                            count--;
                            Resize_Arrays("DOWN");
                        }
                    }
                    else if (x.InnerText == "Name")
                    {
                        Song[count, 0] = (x.NextSibling.InnerText);
                        count++;
                        Resize_Arrays("UP");
                    }
                    else if (x.InnerText == "Artist")
                        Song[count - 1,1] = (x.NextSibling.InnerText);
                    else if (x.InnerText == "Album")
                        Song[count - 1,2] = (x.NextSibling.InnerText);
                    else if (x.InnerText == "Genre")
                        Song[count - 1,3] = (x.NextSibling.InnerText);
                    else if (x.InnerText == "Location")
                        Song[count - 1,4] = (x.NextSibling.InnerText);
                    else if (x.InnerText == "Total Time")
                        Song[count - 1,5] = Convert.ToString(Convert.ToInt32(x.NextSibling.InnerText) / 1000);
                    else if (x.InnerText == "Play Count")
                        Song[count - 1,6] = Convert.ToString(Convert.ToInt32(x.NextSibling.InnerText));
                }
            }

            if (Song[Song.GetLength(0) - 1, 0] == "")
                Resize_Arrays("DOWN");

            File.Delete(tmpLib);
        }
        private void Resize_Arrays(string Direction)
        {
            string[,] oldSong = new string[Song.GetLength(0), 7];

            if (Direction == "UP")
            {
                for (int i = 0; i < Song.GetLength(0); i++)
                {
                    oldSong[i, 0] = Song[i, 0];
                    oldSong[i, 1] = Song[i, 1];
                    oldSong[i, 2] = Song[i, 2];
                    oldSong[i, 3] = Song[i, 3];
                    oldSong[i, 4] = Song[i, 4];
                    oldSong[i, 5] = Song[i, 5];
                    oldSong[i, 6] = Song[i, 6];
                }
                Song = new string[Song.GetLength(0) + 1, 7];
                for (int i = 0; i < oldSong.GetLength(0); i++)
                {
                    Song[i, 0] = oldSong[i, 0];
                    Song[i, 1] = oldSong[i, 1];
                    Song[i, 2] = oldSong[i, 2];
                    Song[i, 3] = oldSong[i, 3];
                    Song[i, 4] = oldSong[i, 4];
                    Song[i, 5] = oldSong[i, 5];
                    Song[i, 6] = oldSong[i, 6];
                }
                Song[Song.GetLength(0) - 1, 0] = "";
                Song[Song.GetLength(0) - 1, 1] = "";
                Song[Song.GetLength(0) - 1, 2] = "";
                Song[Song.GetLength(0) - 1, 3] = "";
                Song[Song.GetLength(0) - 1, 4] = "";
                Song[Song.GetLength(0) - 1, 5] = "0";
                Song[Song.GetLength(0) - 1, 6] = "0";
            }
            else
            {
                for (int i = 0; i < Song.GetLength(0); i++)
                {
                    oldSong[i, 0] = Song[i, 0];
                    oldSong[i, 1] = Song[i, 1];
                    oldSong[i, 2] = Song[i, 2];
                    oldSong[i, 3] = Song[i, 3];
                    oldSong[i, 4] = Song[i, 4];
                    oldSong[i, 5] = Song[i, 5];
                    oldSong[i, 6] = Song[i, 6];
                }
                Song = new string[Song.GetLength(0) - 1, 7];
                for (int i = 0; i < Song.GetLength(0); i++)
                {
                    Song[i, 0] = oldSong[i, 0];
                    Song[i, 1] = oldSong[i, 1];
                    Song[i, 2] = oldSong[i, 2];
                    Song[i, 3] = oldSong[i, 3];
                    Song[i, 4] = oldSong[i, 4];
                    Song[i, 5] = oldSong[i, 5];
                    Song[i, 6] = oldSong[i, 6];
                }
            }
        }
    }
}
